package com.dawn.module.hospital.service;

import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.module.hospital.service.ehealth.HealthCardService;
import com.dawn.module.hospital.service.ehealth.bean.QRCodeReq;
import com.dawn.module.hospital.service.ehealth.bean.ReportDataReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/10/18 10:30
 */
@Slf4j
@Service
public class EhealthService extends BHService {

    @Resource
    @Lazy
    private HealthCardService healthCardService;

    public void uploadData(String ehealthCardId,String cardNo,String scene,String department) {
        QRCodeReq codeReq = new QRCodeReq();
        if (getEhealthCfg() != null) {
            codeReq.setAppId(getEhealthCfg().getAppId());
            codeReq.setAppSecret(getEhealthCfg().getAppSecret());
            codeReq.setHospitalId(getEhealthCfg().getHospitalId());
        }
        codeReq.setHealthCardId(ehealthCardId);
        codeReq.setIdNumber(cardNo);
        CommonResult qrCodeResult = healthCardService.getQRCode(codeReq);
        if(qrCodeResult.isSuccess()) {
            JSONObject codeResultData = (JSONObject)qrCodeResult.getData();
            if(codeResultData.containsKey("qrCodeText")) {
                String qrCodeText = String.valueOf(codeResultData.get("qrCodeText"));
                ReportDataReq reportDataReq = new ReportDataReq();
                if (getEhealthCfg() != null) {
                    reportDataReq.setAppId(getEhealthCfg().getAppId());
                    reportDataReq.setAppSecret(getEhealthCfg().getAppSecret());
                    reportDataReq.setHospitalId(getEhealthCfg().getHospitalId());
                    reportDataReq.setQrCodeText(qrCodeText);
                    reportDataReq.setScene(scene);
                    if (StringUtils.isNotBlank(department)) {
                        reportDataReq.setDepartment(department);
                    }
                    healthCardService.uploadData(reportDataReq);
                }
            }
        }
    }
}
